package cn.yuanqiao.system.service.impl;


import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.system.domain.SysPasswordRule;
import cn.yuanqiao.system.mapper.SysPasswordRuleMapper;
import cn.yuanqiao.system.service.ISysPasswordRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zjj
 * @date 2023/8/2
 */
@Service
public class SysPasswordRuleServiceImpl implements ISysPasswordRuleService {
    @Autowired
    private SysPasswordRuleMapper sysPasswordRuleMapper;
    @Override
    public int insertSysPasswordRule(SysPasswordRule sysPasswordRule) {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            //没有的统一使用-1
            tenantCode = "-1";
        }
        sysPasswordRule.setTenantCode(tenantCode);
        return sysPasswordRuleMapper.insertSysPasswordRule(sysPasswordRule);
    }

    @Override
    public int updateSysPasswordRule(SysPasswordRule sysPasswordRule) {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            //没有的统一使用-1
            tenantCode = "-1";
        }
        sysPasswordRule.setTenantCode(tenantCode);
        return sysPasswordRuleMapper.updateSysPasswordRule(sysPasswordRule);
    }

    @Override
    public SysPasswordRule selectByTenantCode() {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            //没有的统一使用-1
            tenantCode = "-1";
        }
        return sysPasswordRuleMapper.selectByTenantCode(tenantCode);
    }

    @Override
    public boolean verifyPwd(String pwd) {
        String tenantCode = SecurityUtils.getTenantCode();
        SysPasswordRule passwordRule = null;
        if(StringUtils.isNotEmpty(tenantCode)){
            passwordRule = sysPasswordRuleMapper.selectByTenantCode(tenantCode);
        }else {
            passwordRule = sysPasswordRuleMapper.selectByTenantCode("-1");
        }
        if(passwordRule != null){
            //验证最小长度
            if(passwordRule.getPwdMinLength()!=null && passwordRule.getPwdMinLength()!=0){
                if(pwd.length()<passwordRule.getPwdMinLength()){//10<12
                    throw new ServiceException("密码长度不能小于"+passwordRule.getPwdMinLength()+"位！");
                }
            }
            if(passwordRule.getPwdMaxLength()!=null && passwordRule.getPwdMaxLength()!=0){
                if(pwd.length()>passwordRule.getPwdMaxLength()){  //12>10
                    throw new ServiceException("密码长度不能大于"+passwordRule.getPwdMaxLength()+"位！");
                }
            }
            if(passwordRule.getPwdUppercase()!=null && passwordRule.getPwdUppercase() == 1){
                //大写字母
                String regex = "[A-Z]";
                // 创建Pattern对象
                Pattern pattern = Pattern.compile(regex);
                // 创建Matcher对象
                Matcher matcher = pattern.matcher(pwd);
                if(!matcher.find()){
                    throw new ServiceException("密码必须包含大写字母！");
                }
            }
            if(passwordRule.getPwdLowercase()!=null && passwordRule.getPwdLowercase() == 1){
                //小写字母
                String regex = "[a-z]";
                // 创建Pattern对象
                Pattern pattern = Pattern.compile(regex);
                // 创建Matcher对象
                Matcher matcher = pattern.matcher(pwd);
                if(!matcher.find()){
                    throw new ServiceException("密码必须包含小写字母！");
                }
            }
            if(passwordRule.getPwdNumber()!=null && passwordRule.getPwdNumber() == 1){
                //大写字母
                String regex = "[0-9]";
                // 创建Pattern对象
                Pattern pattern = Pattern.compile(regex);
                // 创建Matcher对象
                Matcher matcher = pattern.matcher(pwd);
                if(!matcher.find()){
                    throw new ServiceException("密码必须包含数字！");
                }
            }
            if(passwordRule.getPwdSpecial()!=null && passwordRule.getPwdSpecial() == 1){
                //大写字母
                String regex = "[_!@#$]";
                // 创建Pattern对象
                Pattern pattern = Pattern.compile(regex);
                // 创建Matcher对象
                Matcher matcher = pattern.matcher(pwd);
                if(!matcher.find()){
                    throw new ServiceException("密码必须包含特殊字符(_!@#$)！");
                }
            }
        }
        return true;
    }

    @Override
    public void savepwdrule(SysPasswordRule passwordRule) {
        String tenantCode = SecurityUtils.getTenantCode();
        if(StringUtils.isEmpty(tenantCode)){
            //没有的统一使用-1
            tenantCode = "-1";
        }
        passwordRule.setTenantCode(tenantCode);
        SysPasswordRule data = sysPasswordRuleMapper.selectByTenantCode(tenantCode);
        if(data!=null){
            sysPasswordRuleMapper.updateSysPasswordRule(passwordRule);
        }else {
            sysPasswordRuleMapper.insertSysPasswordRule(passwordRule);
        }
    }
}
